package com.suda.controller;

import com.github.pagehelper.PageInfo;
import com.suda.pojo.Route;
import com.suda.service.RouteService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:zhuyongjie
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/route")
@CrossOrigin
public class RouteController {

    @Autowired
    private RouteService routeService;

    /***
     * Route分页条件搜索实现
     * @param route
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) Route route, @PathVariable  int page, @PathVariable  int size){
        //调用RouteService实现分页条件查询Route
        PageInfo<Route> pageInfo = routeService.findPage(route, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Route分页搜索实现
     * @param page:当前页
     * @param limit:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search" )
    public Result<PageInfo> findPage(int page, int limit){
        //调用RouteService实现分页查询Route
        PageInfo<Route> pageInfo = routeService.findPage(page, limit);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param route
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<Route>> findList(@RequestBody(required = false) Route route){
        //调用RouteService实现条件查询Route
        List<Route> list = routeService.findList(route);
        return new Result<List<Route>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用RouteService实现根据主键删除
        routeService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Route数据
     * @param route
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Route route, @PathVariable String id){
        //设置主键值
        route.setRouteId(id);
        //调用RouteService实现修改Route
        routeService.update(route);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Route数据
     * @param route
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Route route){
        //调用RouteService实现添加Route
        routeService.add(route);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Route数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Route> findById(@PathVariable String id){
        //调用RouteService实现根据主键查询Route
        Route route = routeService.findById(id);
        return new Result<Route>(true,StatusCode.OK,"查询成功",route);
    }

    /***
     * 查询Route全部数据
     * @return
     */
    @GetMapping
    public Result<List<Route>> findAll(){
        //调用RouteService实现查询所有Route
        List<Route> list = routeService.findAll();
        return new Result<List<Route>>(true, StatusCode.OK,"查询成功",list) ;
    }
}
